package com.fengchao.util.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 发邮件工具
 * Created by wangnian on 2016/7/7.
 */
@Component
public class MailTools {
    @Autowired
    private Environment env;
    @Autowired
    private JavaMailSender mailSender;
    private static Logger logger = LoggerFactory.getLogger(MailTools.class);

    @Async
    public void SendMail(String title, String text) {
        logger.info("发邮件中");
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(env.getProperty("spring.mail.username"));
            message.setTo(env.getProperty("spring.mail.sendTo"));
            message.setSubject(env.getProperty("spring.profiles.active") + title);
            message.setText(text);
            mailSender.send(message);
        } catch (Exception e) {
            logger.warn("发送邮件消息失败=exception:{}", e);
        }
    }
}